#导入蓝图
from flask import Blueprint

#实例化蓝图
coupon = Blueprint('coupon',__name__)

#导入请求
from flask import  request

#导入数据库
from db import Db

#导入json 解决二进制流转成python格式
import json

#导入时间 随机数库
import  datetime,random

@coupon.route('/add/',methods=['GET','POST'])
async def add():
    if  request.method == 'POST':
        #获取数据 获取到的是二进制流转成python
        data = json.loads(request.data)
        print(data)
        #构造code
        code = datetime.datetime.now().strftime('%Y%m%d%H%M%S')+str(random.randint(1000,9999))
        # 写入数据库
        sql = "insert into coupon (`code`,`man`,`jian`,`stime`,`etime`,`count`) values ('%s','%d','%d','%s','%s','%d')" \
              % (code, int(data['man']), int(data['jian']), data['stime'], data['etime'],int(data['count']))

        # 实例化数据库
        try:
            db = Db()
            # 加入数据库
            db.insert(sql)
            return {'code':200 , 'msg':'添加成功'}
        except Exception as e:
            return {'code':403 , 'msg':'添加失败'}

    else:
        return '获取消息'